package com.nikmesoft.android.ebox.activities;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.nikmesoft.android.ebox.R;
import com.nikmesoft.android.ebox.helpers.SharedPreferencesHelper;
import com.nikmesoft.android.ebox.utils.LockPatternUtil;
import com.nikmesoft.android.ebox.views.LockPatternView;
import com.nikmesoft.android.ebox.views.LockPatternView.Cell;
import com.nikmesoft.android.ebox.views.LockPatternView.DisplayMode;

public class FirstUseActivity extends BaseActivity {
	private TextView tvInfo;
	private LockPatternView mLockPatternView;
	private LinearLayout bottomBar;
	private Button btnCancel;
	private Button btnContinue;
	private List<Cell> mLastPattern;
	private LPMode mMode;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_firstuse);
		init();
	}

	private void init() {
		CharSequence info = tvInfo != null ? tvInfo.getText() : null;
		CharSequence btnConfirmText = btnContinue != null ? btnContinue
				.getText() : null;
		Boolean btnConfirmEnabled = btnContinue != null ? btnContinue
				.isEnabled() : null;
		LockPatternView.DisplayMode lastDisplayMode = mLockPatternView != null ? mLockPatternView
				.getDisplayMode() : null;
		List<Cell> lastPattern = mLockPatternView != null ? mLockPatternView
				.getPattern() : null;

		tvInfo = (TextView) findViewById(R.id.tvInfo);
		mLockPatternView = (LockPatternView) findViewById(R.id.lockPatternView);
		mLockPatternView.setOnPatternListener(mLockPatternViewListener);
		bottomBar = (LinearLayout) findViewById(R.id.bottom_bar);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnContinue = (Button) findViewById(R.id.btnContinue);
		if (mMode == null) {
			mMode = LPMode.CreatePattern;
		}
		if (lastPattern != null && lastDisplayMode != null)
			mLockPatternView.setPattern(lastDisplayMode, lastPattern);

		switch (mMode) {
		case CreatePattern:
			btnCancel.setOnClickListener(btnCancelOnClickListener);
			btnContinue.setOnClickListener(btnConfirmOnClickListener);

			bottomBar.setVisibility(View.VISIBLE);

			if (info != null)
				tvInfo.setText(info);
			else
				tvInfo.setText(R.string.lockpattern_msg_draw_an_unlock_pattern);

			if (btnConfirmText != null) {
				btnContinue.setText(btnConfirmText);
				btnContinue.setEnabled(btnConfirmEnabled);
			}

			break;// CreatePattern

		case ComparePattern:
			bottomBar.setVisibility(View.GONE);

			if (info != null)
				tvInfo.setText(info);
			else
				tvInfo.setText(R.string.lockpattern_msg_draw_pattern_to_unlock);

			break;
		}

		setResult(RESULT_CANCELED);
	}

	private String encodePattern(List<Cell> pattern) {
		return LockPatternUtil.patternToSha1(pattern);
	}

	private int mRetryCount = 0;

	private void doComparePattern(List<Cell> pattern) {
		if (pattern == null)
			return;

		mLastPattern = new ArrayList<LockPatternView.Cell>();
		mLastPattern.addAll(pattern);

		String currentPattern = SharedPreferencesHelper.getInstance()
				.getPasscode();
		if (encodePattern(pattern).equals(currentPattern)) {
			btnContinue.setText(R.string.activity_firstuse_button_confirm);
			btnContinue.setEnabled(true);
		} else {
			mRetryCount++;

			if (mRetryCount >= 3) {
				finish();
			} else {
				mLockPatternView.setDisplayMode(DisplayMode.NotMatch);
				tvInfo.setText(R.string.lockpattern_msg_try_again);
			}
		}
	}

	private void doCreatePattern(List<Cell> pattern) {
		if (pattern.size() < 4) {
			mLockPatternView.setDisplayMode(DisplayMode.NotMatch);
			tvInfo.setText(getString(R.string.lockpattern_msg_connect_4dots));
			return;
		}

		if (mLastPattern == null) {
			mLastPattern = new ArrayList<LockPatternView.Cell>();
			mLastPattern.addAll(pattern);
			tvInfo.setText(getString(R.string.lockpattern_msg_pattern_recorded));
			btnContinue.setEnabled(true);
		} else {
			btnContinue.setEnabled(false);
			mLockPatternView.setDisplayMode(DisplayMode.NotMatch);
		}
	}

	private final LockPatternView.OnPatternListener mLockPatternViewListener = new LockPatternView.OnPatternListener() {

		public void onPatternStart() {
			mLockPatternView.setDisplayMode(DisplayMode.Match);

			if (mMode == LPMode.CreatePattern) {
				tvInfo.setText(R.string.lockpattern_msg_release_finger_when_done);
				btnContinue.setEnabled(false);
				if (getString(R.string.activity_firstuse_button_continue)
						.equals(btnContinue.getText()))
					mLastPattern = null;
			}
		}

		public void onPatternDetected(List<Cell> pattern) {
			switch (mMode) {
			case CreatePattern:
				doCreatePattern(pattern);
				break;
			case ComparePattern:
				doComparePattern(pattern);
				break;
			}
		}

		public void onPatternCleared() {
			mLockPatternView.setDisplayMode(DisplayMode.Match);

			switch (mMode) {
			case CreatePattern:
				btnContinue.setEnabled(false);
				if (getString(R.string.activity_firstuse_button_continue)
						.equals(btnContinue.getText())) {
					mLastPattern = null;
					tvInfo.setText(R.string.lockpattern_msg_draw_an_unlock_pattern);
				} else
					tvInfo.setText(R.string.lockpattern_msg_redraw_pattern_to_confirm);
				break;
			case ComparePattern:
				tvInfo.setText(R.string.lockpattern_msg_draw_pattern_to_unlock);
				break;
			}
		}

		public void onPatternCellAdded(List<Cell> pattern) {

		}
	};

	public static enum LPMode {
		CreatePattern, ComparePattern
	}

	private final View.OnClickListener btnCancelOnClickListener = new View.OnClickListener() {

		public void onClick(View v) {
			// finish();
			Intent intent = new Intent(FirstUseActivity.this,
					MainActivity.class);
			startActivity(intent);
		}
	};

	private final View.OnClickListener btnConfirmOnClickListener = new View.OnClickListener() {

		public void onClick(View v) {
			if (getString(R.string.activity_firstuse_button_continue).equals(
					btnContinue.getText())) {
				mLockPatternView.clearPattern();
				tvInfo.setText(R.string.lockpattern_msg_redraw_pattern_to_confirm);
				btnContinue.setText(R.string.activity_firstuse_button_continue);
				btnContinue.setEnabled(false);
			} else {
				SharedPreferencesHelper.getInstance().setPasscode(
						encodePattern(mLastPattern));
				Intent intent = new Intent(FirstUseActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		}
	};
}
